package stackdemo;

import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 黎鹤舞
 * Date: 2024-01-16
 * Time: 0:14
 */
class MinStack {

    Stack<Integer> stack;
    Stack<Integer> minStack;


    //构造函数
    public MinStack() {
        this.stack = new Stack<>();
        this.minStack = new Stack<>();
    }


    //压栈、入栈
    public void push(int val) {
        stack.push(val);
        //如果是第一个元素，必须要压栈
        if(minStack.empty()) {
            minStack.push(val);
        }else {
            int temp = minStack.peek();
            //注意:如果这里val == 栈顶元素，同样需要压栈
            if(val <= temp) {
                minStack.push(val);
            }
        }
    }



    //出栈
    public void pop() {
        if(stack.peek().equals(minStack.peek())) {
            minStack.pop();
        }
        stack.pop();
    }


    public int top() {
        return stack.peek();
    }


    public int getMin() {
        return minStack.peek();
    }
}